package per.kongtouxu.service.Impl;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import per.kongtouxu.dao.AdminLogDao;
import per.kongtouxu.domain.pojo.AdminLog;
import per.kongtouxu.domain.pojo.PageBean;
import per.kongtouxu.domain.vo.V_AdminLog;
import per.kongtouxu.service.AdminLogService;

import java.util.ArrayList;
import java.util.List;
@Service
public class AdminLogServiceImpl implements AdminLogService {
    @Autowired
    private AdminLogDao adminLogDao;
    @Override
    public Integer saveAdminLog(AdminLog adminLog) {

        Integer result = adminLogDao.saveAdminLog(adminLog);
        return result;
    }

    @Override
    public PageBean<V_AdminLog> findAdminLogByPage(String startTime, String endTime, String cp, String ps) {
       Integer currentPage = Integer.parseInt(cp);
       Integer pageSize=Integer.parseInt(ps);

        //1.分页类
        PageBean<V_AdminLog> pageBean = new PageBean<>();
        //2.设置当前页码
        pageBean.setCurrentPage(currentPage);
        //3.设置每页显示条数
        pageBean.setPageSize(pageSize);
        //4.设置总记录条数
        int totalCount = adminLogDao.findCountAdminLogByPage(startTime, endTime);
        pageBean.setTotalCount(totalCount);
        //5.设置PageBean中的实体数据
        //开始的记录条数=（当前页码-1）*每页显示条数
        int start = (currentPage - 1) * pageSize;
        List<V_AdminLog> list = adminLogDao.findAdminLogByPage(startTime, endTime, new RowBounds(start, pageSize));
        pageBean.setList(list);
        //6.设置总页数   总页数= 总记录条数/每页显示条数=0 ？ 总记录条数/每页显示条数=0 ：（总记录条数/每页显示条数=0）+1
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pageBean.setTotalPage(totalPage);
        //7.返回
        return pageBean;

    }

    @Override
    public int deleteSome(String[] list) {
        //1.转换为集合
        ArrayList<Integer> logsList = new ArrayList<>();
        for (String s : list) {
            logsList.add(Integer.parseInt(s));
        }
        //2.调用dao
        Integer result = adminLogDao.deleteSome(logsList);
        return result;
    }


}
